package com.teach.controller;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import com.teach.dto.AuthUser;
import com.teach.dto.ReturnData;
import com.teach.service.UserService;
import com.teach.util.JwtUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 * TODO : 认证中心核心控制器
 * ClassName : com.teach.controller.AuthController
 * Author : J.L.Zhou
 * Date : 2020-03-06 18:54
 * Version : V1.0
 * Copyright 2020 jlzhou.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes</pre>
 */
@RestController
@Slf4j
public class AuthController {
	
	public static final String PREFIX = "Bearer ";
	
	@Autowired
	private UserService service;
	
	@Autowired
	private JwtUtil jwtUtil;

	@GetMapping("/login")
	public ReturnData<String> doLogin(String userName,String userPwd){
		log.debug("登录：{} - {}",userName,userPwd);
		try {
			AuthUser user = service.login(userName, userPwd);
			return new ReturnData<String>(200, "ok", jwtUtil.createJWT(UUID.randomUUID().toString(), "J.L.Zhou", user));
		}catch (Exception e) {
			return new ReturnData<String>(HttpStatus.UNAUTHORIZED.value(), e.getMessage(), null);
		}
	}
	
	@GetMapping("/auth-login")
	public ReturnData<AuthUser> doAuthLogin(String userName,String userPwd){
		log.debug("登录：{} - {}",userName,userPwd);
		try {
			AuthUser user = service.login(userName, userPwd);
			return new ReturnData<AuthUser>(200,"ok",user);
		}catch (Exception e) {
			return new ReturnData<AuthUser>(HttpStatus.UNAUTHORIZED.value(), e.getMessage(), null);
		}
	}
	
	
	@GetMapping("/list")
	public ReturnData<List<AuthUser>> doList(){
		return new ReturnData<List<AuthUser>>(200, "ok", service.listAll());
	}
	
	@GetMapping("/get/{id}")
	public ReturnData<AuthUser> doGet(@PathVariable("id")Long id){
		return new ReturnData<AuthUser>(200, "ok", service.get(id));
	}
	
}
